<script setup>
import gameData from '@/common/data.js';
import {
  capitalizeFirstLetter,
  categoryList,
  getQuery,
  // getTopLevelDomain,
  // toQuery,
} from '@/common/utils.js';
import { computed, ref, watch, nextTick } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import GameItem from './GItem.vue';

const route = useRoute();
const router = useRouter();

const getNav = () => capitalizeFirstLetter(route.path?.split('/')[2]);
const category = ref(getNav());

// 分类导航容器引用
const categoryNavContainer = ref(null);

const gameList = computed(() => {
  return gameData[category.value] || [];
});

const handleCategoryClick = (cat) => {
  const query = getQuery(`/games/${cat}`);
  if (route.name === 'Games' && route.path === query.path) {
    window.scrollTo({
      top: 0,
      behavior: 'smooth',
    });
    return;
  }
  router.push(query);
};

const handleLogoClick = () => {
  router.push(getQuery('/'));
};

const handleSearchClick = () => {
  router.push(getQuery('/search'));
};

// 滚动到当前选中的分类
const scrollToActiveCategory = () => {
  nextTick(() => {
    if (!categoryNavContainer.value) return;
    const activeItem = categoryNavContainer.value.querySelector('.category-nav-item.active');
    if (activeItem) {
      activeItem.scrollIntoView({
        behavior: 'smooth',
        block: 'nearest',
        inline: 'center',
      });
    }
  });
};

// 监听路由变化，自动滚动到当前分类
watch(
  () => route.path,
  () => {
    category.value = getNav();
    scrollToActiveCategory();
    window.scrollTo({
      top: 0,
      behavior: 'instant',
    });
  },
  { immediate: true },
);
</script>

<template>
  <div class="category-page-wrap">
    <!-- 顶部分类导航 -->
    <div class="category-nav">
      <!-- 顶部第一行：Logo和操作按钮 -->
      <div class="nav-top-row">
        <!-- Logo区域 -->
        <div class="nav-logo" @click="handleLogoClick">
          <img src="@/assets/img/logo.png" alt="Puzzle Games Pro" />
          <span>Puzzle Games Pro</span>
        </div>

        <!-- 操作按钮容器 -->
        <div class="nav-actions">
          <!-- 首页图标 -->
          <div class="nav-home" @click="handleLogoClick" title="返回首页">
            <svg
              width="18"
              height="18"
              viewBox="0 0 24 24"
              fill="none"
              xmlns="http://www.w3.org/2000/svg"
            >
              <path
                d="M3 12L5 10M5 10L12 3L19 10M5 10V20C5 20.5523 5.44772 21 6 21H9M19 10L21 12M19 10V20C19 20.5523 18.5523 21 18 21H15M9 21C9.55228 21 10 20.5523 10 20V16C10 15.4477 10.4477 15 11 15H13C13.5523 15 14 15.4477 14 16V20C14 20.5523 14.4477 21 15 21M9 21H15"
                stroke="currentColor"
                stroke-width="2"
                stroke-linecap="round"
                stroke-linejoin="round"
              />
            </svg>
          </div>

          <!-- 搜索图标 -->
          <div class="nav-search" @click="handleSearchClick" title="搜索">
            <img src="@/assets/img/search.svg" alt="Search" />
          </div>
        </div>
      </div>

      <!-- 第二行：分类导航容器 -->
      <div class="category-nav-container" ref="categoryNavContainer">
        <div
          class="category-nav-item"
          v-for="cat in categoryList"
          :key="cat"
          :class="{ active: cat === category }"
          @click="handleCategoryClick(cat)"
        >
          <span>{{ cat }}</span>
        </div>
      </div>
    </div>

    <div class="game-content">
      <!-- 分类标题 -->
      <div class="category-title">
        <span>{{ category }}</span>
        <span>Games</span>
      </div>

      <div class="game-list">
        <div
          v-for="game in gameList"
          :key="game.name || game.key"
          :class="[
            'game-item-wrapper',
            {
              hide: !game.name || game.hidden,
            },
          ]"
        >
          <GameItem :game="game" class="game-item" />
          <div class="game-name">{{ game.name }}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.category-page-wrap {
  position: relative;
  width: 100%;
  min-height: 100vh;
  overflow-x: hidden;
  color: #fff;

  // 顶部分类导航
  .category-nav {
    position: sticky;
    top: 0;
    z-index: 100;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
    padding: 0;
    display: flex;
    flex-direction: column;
    min-height: auto;

    // 顶部第一行：Logo和操作按钮
    .nav-top-row {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 10px 12px;
      gap: 12px;
      min-height: 48px;
    }

    // Logo区域
    .nav-logo {
      flex: 1;
      display: flex;
      align-items: center;
      gap: 6px;
      cursor: pointer;
      transition: opacity 0.2s ease;
      min-width: 0;

      &:active {
        opacity: 0.7;
      }

      img {
        height: 20px;
        width: auto;
        flex-shrink: 0;
      }

      span {
        font-size: 12px;
        color: #fff;
        font-weight: 600;
        -webkit-text-fill-color: transparent;
        -webkit-background-clip: text;
        background-clip: text;
        background-image: linear-gradient(45deg, #4398d1, #fdb62f, #c94536, #88b337);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    // 操作按钮容器（首页和搜索）
    .nav-actions {
      display: flex;
      align-items: center;
      gap: 8px;
      flex-shrink: 0;
    }

    // 首页图标
    .nav-home {
      flex-shrink: 0;
      width: 36px;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.2s ease;
      color: #fff;
      border-radius: 8px;
      background: rgba(255, 255, 255, 0.1);

      &:active {
        transform: scale(0.95);
        background: rgba(255, 255, 255, 0.2);
      }

      svg {
        width: 18px;
        height: 18px;
      }
    }

    // 搜索图标
    .nav-search {
      flex-shrink: 0;
      width: 36px;
      height: 36px;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
      transition: all 0.2s ease;
      border-radius: 8px;
      background: rgba(255, 255, 255, 0.1);

      &:active {
        transform: scale(0.95);
        background: rgba(255, 255, 255, 0.2);
      }

      img {
        width: 18px;
        height: 18px;
      }
    }

    // 分类导航容器（第二行）
    .category-nav-container {
      display: flex;
      align-items: center;
      gap: 6px;
      padding: 8px 12px;
      overflow-x: auto;
      overflow-y: hidden;
      -webkit-overflow-scrolling: touch;
      scrollbar-width: none;
      -ms-overflow-style: none;

      &::-webkit-scrollbar {
        display: none;
      }

      .category-nav-item {
        flex-shrink: 0;
        padding: 6px 12px;
        background: rgba(255, 255, 255, 0.2);
        border-radius: 8px;
        cursor: pointer;
        transition: all 0.3s ease;
        white-space: nowrap;
        backdrop-filter: blur(10px);
        display: flex;
        align-items: center;
        justify-content: center;
        text-align: center;
        min-height: 28px;
        position: relative;

        &:active {
          transform: scale(0.95);
        }

        // 激活状态样式
        &.active {
          background: rgba(255, 255, 255, 0.35);
          box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);

          span {
            font-weight: 600;
            color: #fff;
          }

          // 添加底部指示线
          &::after {
            content: '';
            position: absolute;
            bottom: 2px;
            left: 50%;
            transform: translateX(-50%);
            width: 60%;
            height: 2px;
            background: rgba(255, 255, 255, 0.9);
            border-radius: 1px;
          }
        }

        span {
          font-size: 11px;
          color: rgba(255, 255, 255, 0.9);
          font-weight: 500;
          text-align: center;
          transition: all 0.3s ease;
        }
      }
    }
  }

  .game-content {
    max-width: 1600px;
    margin: 0 auto;
    padding: 0 0 20px 0;
    min-height: 100vh;
    overflow-x: hidden;
    box-sizing: border-box;

    // 分类标题
    .category-title {
      margin: 16px 0;
      padding: 0 12px;
      text-align: center;

      span {
        display: block;
        font-size: 16px;
        font-weight: 600;
        color: #fff;

        &:nth-of-type(2) {
          font-size: 11px;
          opacity: 0.8;
          margin-top: 4px;
        }
      }
    }

    .game-list {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 8px;
      padding: 0 12px;
      margin: 10px 0;
      box-sizing: border-box;

      .game-item-wrapper {
        display: flex;
        flex-direction: column;
        align-items: center;
        width: 100%;
        box-sizing: border-box;

        &.hide {
          display: none;
        }

        .game-item {
          position: relative;
          display: flex;
          justify-content: center;
          align-items: center;
          aspect-ratio: 1;
          overflow: hidden;
          width: 100%;
          max-width: 50px;
          box-sizing: border-box;
        }

        .game-name {
          margin-top: 4px;
          font-size: 10px;
          color: #fff;
          text-align: center;
          line-height: 1.2;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          line-clamp: 2;
          -webkit-box-orient: vertical;
          word-break: break-word;
          width: 100%;
          max-width: 60px;
        }
      }
    }
  }

  // PC端适配 (1025px及以上，iPad使用移动端布局)
  @media (min-width: 1025px) {
    .category-nav {
      flex-direction: row;
      padding: 5px 10px;
      min-height: 40px;
      align-items: center;
      gap: 6px;
      overflow: hidden;

      .nav-top-row {
        flex-shrink: 0;
        padding: 0;
        min-height: auto;
        gap: 4px;
        display: flex;
        align-items: center;
      }

      .nav-logo {
        gap: 2px;
        flex-shrink: 0;
        max-width: 100px;

        img {
          height: 10px;
        }

        span {
          font-size: 9px;
        }
      }

      .nav-actions {
        gap: 4px;
        flex-shrink: 0;
      }

      .nav-home {
        width: 24px;
        height: 24px;
        flex-shrink: 0;

        svg {
          width: 12px;
          height: 12px;
        }
      }

      .category-nav-container {
        flex: 1;
        min-width: 0;
        padding: 0;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: thin;
        scrollbar-color: rgba(255, 255, 255, 0.3) transparent;
        justify-content: flex-start;
        gap: 2px;

        &::-webkit-scrollbar {
          height: 4px;
        }

        &::-webkit-scrollbar-track {
          background: transparent;
        }

        &::-webkit-scrollbar-thumb {
          background: rgba(255, 255, 255, 0.3);
          border-radius: 2px;

          &:hover {
            background: rgba(255, 255, 255, 0.5);
          }
        }

        .category-nav-item {
          padding: 2px 6px;
          min-height: 22px;
          flex-shrink: 0;
          position: relative;

          &.active {
            background: rgba(255, 255, 255, 0.35);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);

            span {
              font-weight: 600;
            }

            &::after {
              content: '';
              position: absolute;
              bottom: 1px;
              left: 50%;
              transform: translateX(-50%);
              width: 60%;
              height: 1.5px;
              background: rgba(255, 255, 255, 0.9);
              border-radius: 1px;
            }
          }

          span {
            font-size: 8px;
          }
        }
      }

      .nav-search {
        width: 24px;
        height: 24px;
        flex-shrink: 0;

        img {
          width: 12px;
          height: 12px;
        }
      }
    }

    .game-content {
      padding: 0 50px;

      .category-title {
        margin: 30px 0;
        padding: 0 50px;

        span {
          font-size: 24px;

          &:nth-of-type(2) {
            font-size: 16px;
            margin-top: 6px;
          }
        }
      }

      .game-list {
        grid-template-columns: repeat(9, 1fr);
        gap: 5px;
        padding: 0 50px;

        .game-item-wrapper {
          .game-item {
            max-width: 40px;
          }

          .game-name {
            font-size: 5px;
            margin-top: 2px;
            max-width: 50px;
          }
        }
      }
    }
  }

  // 大屏PC端适配
  @media (min-width: 1024px) {
    .game-content {
      padding: 0 50px;

      .game-list {
        grid-template-columns: repeat(9, 1fr);
        gap: 6px;
        padding: 0 50px;

        .game-item-wrapper {
          .game-item {
            max-width: 50px;
          }

          .game-name {
            font-size: 6px;
            margin-top: 3px;
            max-width: 60px;
          }
        }
      }
    }
  }

  // 超大屏PC端适配
  @media (min-width: 1440px) {
    .game-content {
      padding: 0 60px;

      .game-list {
        grid-template-columns: repeat(9, 1fr);
        gap: 8px;
        padding: 0 60px;

        .game-item-wrapper {
          .game-item {
            max-width: 60px;
          }

          .game-name {
            font-size: 8px;
            margin-top: 4px;
            max-width: 70px;
          }
        }
      }
    }
  }
}
</style>
